package com.hongtech.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hongtech.common.service.impl.CrudServiceImpl;
import com.hongtech.modules.system.dto.SysRoleDTO;
import com.hongtech.modules.system.entity.SysRoleEntity;
import com.hongtech.modules.system.entity.SysRoleMenuEntity;
import com.hongtech.modules.system.entity.SysUserRoleEntity;
import com.hongtech.modules.system.mapper.SysRoleMapper;
import com.hongtech.modules.system.mapper.SysRoleMenuMapper;
import com.hongtech.modules.system.mapper.SysUserRoleMapper;
import com.hongtech.modules.system.service.SysRoleService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * SysRole 表数据服务层接口实现类
 */
@Service
public class SysRoleServiceImpl extends CrudServiceImpl<SysRoleMapper, SysRoleEntity, SysRoleDTO>
        implements SysRoleService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    @Transactional
    public boolean deleteRoleByIds(List<Long> roleIds) {
        // 删除角色与菜单关联
        LambdaQueryWrapper<SysRoleMenuEntity> roleMenuWrapper = new LambdaQueryWrapper<>();
        roleMenuWrapper.in(SysRoleMenuEntity::getRoleId, roleIds);
        sysRoleMenuMapper.delete(roleMenuWrapper);
        // 删除角色与用户关系
        LambdaQueryWrapper<SysUserRoleEntity> userRoleWrapper = new LambdaQueryWrapper<>();
        userRoleWrapper.in(SysUserRoleEntity::getRoleId, roleIds);
        sysUserRoleMapper.delete(userRoleWrapper);
        return this.removeByIds(roleIds);
    }

}
